<?php
session_start();

require_once "db.php";
include "header.php";

			
if(!isset($_GET["cinema"])){
	$_GET["cinema"]=1;
	$cinema=1;
}
else{ $cinema=$_GET["cinema"];}
		

?><!-- 	<noscript displayResult($cinema);</noscript>	-->


	
	<form action="" name="Select Cinemas" method="GET" >
	<?php 
	$quercount=" select count(*) as cnt FROM cinemas";
	$rescount = db_query($quercount);
	$rll= db_get_row($rescount);
	$cincount=$rll["cnt"];
	?>

	<select name="cinema" id="cinema"  onchange='this.form.submit()'>
		<?php
		echo "<option value='-1'>Select Cinema</option>";
		for($i=1;$i<=$cincount;$i++){
			$query2 = "SELECT * FROM cinemas WHERE  id=$i LIMIT 1"; //LIMIT 1 is for optimization
			$result2 = db_query($query2);
			$row2= db_get_row($result2);
			echo "<option value=\"".$row2["id"]."\">".$row2["name"]."</option>";
		 }
		?>
	</select></td></tr>
	<noscript><input type="submit" value="Submit" ></noscript>
	</form>
	
	<table border='1' cellpadding="5" style=" margin-left:auto; margin-right:auto; width:70%;">
		<tr>
		
				<?php for($i=0;$i<=4;$i++){
				$dati[$i]= date('Y-m')."-".(date('d')+"$i");?>
				<td>
					<a href="<?php echo "projections.php?data=".date(('d')+"$i")."&cinema=".$cinema;?> "><?php echo date('Y-m')."-".(date('d')+"$i");?> </a>
				</td>
				<?php
				}
				?>
		</tr>
	</table >
	<table  cellpadding ="1" style=" margin-left:auto; margin-right:auto; width:90%;">
	<tr>
			<td>      </td>
			<td> 10:00</td>
			<td> 11:00</td>
			<td> 12:00</td>
			<td> 13:00</td>
			<td> 14:00</td>
			<td> 15:00</td>
			<td> 16:00</td>
			<td> 17:00</td>
			<td> 18:00</td>
			<td> 19:00</td>
			<td> 20:00</td>
			<td> 21:00</td>
			<td> 22:00</td>
			<td> 23:00</td>
			<td> 24:00</td>
	</tr>
<?php   
		if(!isset($_GET["data"]))
			$_GET["data"]=0;
		
		$quer2="SELECT max(p.id) FROM projection p join movies m on(p.film_id=m.id)
		 		join halls h on(p.hall_id=h.id)
				join cinemas c on(h.cinema_id=c.id) where h.cinema_id=\"".$cinema."\"";
		$dbrec2=db_query($quer2)
		or pretty_die("problem reading table2".mysql_error());
		$maxid=db_get_row($dbrec2);
		$br=$maxid["max(p.id)"];
		
		$quercount=" select count(*) from projection where date(date)="."\"".$dati[$_GET["data"]]."\"";
		$rescount = db_query($quercount);
	 	$r=db_get_row($rescount);
	 	$br=$r["count(*)"];
	 	
	 	$quer2="SELECT max(p.id) FROM projection p join movies m on(p.film_id=m.id) 
						join halls h on(p.hall_id=h.id)
						join cinemas c on(h.cinema_id=c.id)
						where date(p.date)="."\"".$dati[$_GET["data"]]."\" and h.cinema_id=\"".$cinema."\"";///change where cinema = var
		$dbrec2=db_query($quer2)
		or pretty_die("problem reading table2".mysql_error());
		$maxid2=db_get_row($dbrec2);
		for($i=1;$i<=$maxid2["max(p.id)"];$i++){
			
			$quercount=" select  p.film_id, m.name from projection p join movies m on (m.id=p.film_id) join halls h on(p.hall_id=h.id) where date(date)="."\"".$dati[$_GET["data"]]."\" and film_id=$i and h.cinema_id=\"".$cinema."\"";
			$rescount = db_query($quercount);
		 	$r=db_get_row($rescount);
			echo "<tr><td>".$r["name"]."</td>";

			for($k=0;$k<=$maxid2["max(p.id)"];$k++){
				$quer="
				SELECT m.name as filmname,hour(p.date) as hour,minute(p.date) as mins,p.date as date,p.film_id as film_id FROM projection p 
					join movies m on(p.film_id=m.id)
					join halls h on(p.hall_id=h.id)
					join cinemas c on(h.cinema_id=c.id)
                    where p.film_id=$i and p.id=$k and date(date)="."\"".$dati[$_GET["data"]]."\" and h.cinema_id=\"".$cinema."\"";//change moje bi i tuk add where
				$dbrec=db_query($quer)
		 		or pretty_die("problem reading table2".mysql_error());
		 		$prj=db_get_row($dbrec);
				$arr[$i][$k]=$prj["date"];
				$arrh[$i][$k]=$prj["hour"];
				
				if($prj["mins"]==0)
					$arrm[$i][$k]="00";
				else $arrm[$i][$k]=$prj["mins"];
			}
			for($k=0;$k<=$maxid2["max(p.id)"];$k++)
				for($j=10;$j<=24;$j++){
				
		 			if($arrh[$i][$k]==$j ){
		 				$arrprog[$j]=$arrh[$i][$k].":".$arrm[$i][$k];
		 				$arrid[$j]=$k;
		 			}
		 		}
		 	for($j=10;$j<=24;$j++){
				if(isset($arrprog[$j])){
					echo "<td> <form action='saloon.php' method='get'>
								<input type=\"hidden\" name=\"resid\" value=".$arrprog[$j].">
								<input type=\"hidden\" name=\"prid\" value=".$arrid[$j].">
								<input type=\"submit\" value=".$arrprog[$j].">
							</form></td>";
					$arrprog[$j]=null;
				}
				else echo	"<td>  </td>";
			}
			echo "</tr>";
		}
		

?>
	</table>
<?php
include "footer.php";
?>